﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using System.Threading;

namespace TemplateLibraryConnector.Utility
{
    public class Language
    {
        private const string ResourceFileName = "TemplateLibraryConnector";
        private static readonly Dictionary<uint, string> languages = new Dictionary<uint, string>() { { 1033, "English" }, { 1044, "Norwegian" } };

        public static string GetString(string resourceString)
        {
            return GetString(resourceString, ResourceFileName);
        }

        public static string GetString(string resourceString, uint locale)
        {
            return GetString(resourceString, ResourceFileName, locale);
        }

        public static string GetCoreString(string resourceString)
        {
            return GetString(resourceString, "TemplateLibraryConnector");
        }

        public static string GetCoreString(string resourceString, uint locale)
        {
            return GetString(resourceString, "TemplateLibraryConnector", locale);
        }

        public static string GetString(string resourceString, string resourceFile)
        {
            uint locale = GetLocale();

            return GetString(resourceString, resourceFile, locale);
        }

        public static string GetString(string resourceString, string resourceFile, uint locale)
        {
            string source = string.Format("$Resources:{0},{1}", resourceFile, resourceString);
            string localizedString = source;

            try
            {
                localizedString = SPUtility.GetLocalizedString(source, resourceFile, locale);
            }
            catch (Exception ex)
            {
               // SPLogger.Write(ex, additionalInformation: source);
            }

            return localizedString;
        }

        public static uint GetLocale()
        {
            return (uint)Thread.CurrentThread.CurrentUICulture.LCID;
        }

        public static string GetUserLanguage()
        {
            return GetLanguage((uint)System.Threading.Thread.CurrentThread.CurrentUICulture.LCID);
        }

        public static string GetLanguage(uint languageCode)
        {
            return languages[languageCode];
        }
    }
}
